<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <!-- <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item> -->
      <el-breadcrumb-item>公司管理</el-breadcrumb-item>
      <el-breadcrumb-item>公司列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <!-- 搜索 按钮 区域 -->
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入公司名称"
            v-model="queryInfo.query"
            @keyup.enter="getCompanyList()"
            clearable
            @clear="getCompanyList()"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="getCompanyList()"></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="14"></el-col>
        <el-col :span="3">
          <el-button style="width:100%;padding:1px" type="primary" icon="el-icon-plus" @click="addDialogVisible=true">添加</el-button>
        </el-col>
      </el-row>
      <!-- 列表区域 -->
      <el-table :data="companyList" stripe border style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="companyName" label="公司名称"></el-table-column>
        <el-table-column prop="adminPhone" label="管理员账号"></el-table-column>
        <!-- <el-table-column prop="createTime" label="注册时间"></el-table-column> -->
        <el-table-column label="当前状态">
          <template v-slot="scope">
            <span>{{scope.row.companyStatus?'已注销':'使用中'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
                <el-button size="mini" type="danger" icon="el-icon-delete" :disabled="scope.row.status" ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 区域 -->
      <el-config-provider :locale="locale">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-config-provider>
    </el-card>

    <!-- 添加公司 对话框 -->
    <el-dialog v-model="addDialogVisible" title="添加公司" width="50%">
      <!-- 内容主体  添加表单 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="addForm.companyName"></el-input>
        </el-form-item>
        <el-form-item label="管理员手机号" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCompany()">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import zhCn from "element-plus/lib/locale/lang/zh-cn";

export default {
  data() {
    return {
      // 列表的请求 参数对象
      queryInfo: {
        query: "",
        pagenum: 1, //页数
        pagesize: 5 //条数
      },
      // 响应 参数对象
      companyList: [],
      total: 0,
      addDialogVisible: false,
      addForm: {},
      addFormRules: {
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入管理员账号", trigger: "blur" },
        ],
        password:[]
      }
    };
  },
  setup() {
    return {
      locale: zhCn
    };
  },
  mounted() {
    this.getCompanyList();
  },
  methods: {
    async getCompanyList() {
      const { data: res } = await this.$http.get("company/getCompanyList", { params: this.queryInfo });
      console.log(res);
      if (res.code !== 200)
        return this.$message.error(res.msg);
      this.companyList = res.data.list;
      this.total = res.data.total;
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.queryInfo.pagesize = newSize;
      this.getCompanyList();
    },
    // 页码值
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.queryInfo.pagenum = newPage;
      this.getCompanyList();
    },
    async addCompany() {
      this.addDialogVisible = false;
      let formData = new FormData();
      formData.append('companyName',this.addForm.companyName)
      formData.append('adminPhone',this.addForm.phone)
      formData.append('password',this.$md5(this.addForm.password))
      const { data: res } = await this.$http.post("company/addCompany",formData);
      console.log(res);
      if (res.code !== 200) return this.$message.error(res.msg);
      this.getCompanyList();
    }
  }
};
</script>

<style lang="less" scoped>

</style>
